package com.thingsgrid.firmware.controller;

import com.thingsgrid.appmanager.feign.FileFeign;
import com.thingsgrid.firmware.service.FirmwareVersionService;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 功能说明：固件版本管理模块
 */
@RestController
@RequestMapping("/firmware")
@AllArgsConstructor
public class FirmwareVersionController {

	private FirmwareVersionService service;
	/**
	 * 调用文件上传方法
	 */
	private FileFeign fileFeign;

	/**
	 * 查询条件所属类型下拉菜单
	 */
	@PostMapping("/version/productType")
	public R<List> GetProductTypeList() {
		List productTypelist = service.GetProductTypeList();
		return R.data(productTypelist);
	}
	/**
	 *查询条件产品名称下拉菜单
	 * productTypeId是前一个项的返回值
	 */
	@PostMapping("/version/productName")
	public R<List> GetProductName(@RequestParam String productTypeId) {
		List productNamelist = service.GetProductNameList(productTypeId);
		return R.data(productNamelist);
	}
	/**
	 *查询条件固件名称下拉菜单
	 * productName是前一个项的返回值
	 */
	@PostMapping("/version/firmwareName")
	public R<List> GetFirmwareName(@RequestParam String productName) {//刷新返回类型的产品名的map
		List firmwareNamelist = service.GetFirmwareNameList(productName);
		return R.data(firmwareNamelist);
	}
	/**
	 *根据查询条件查询固件表
	 */
	@PostMapping("/version")
	public R<Map> QueryFirmwareListByCondition(@RequestParam String productType, String productName, String firmwareName, int current, int pageSize){
		Map firmwarelist = service.QueryFirmwareListByCondition(productType, productName, firmwareName, current, pageSize);
		return R.data(firmwarelist);
	}
	/**
	 *新增固件中选择所属产品的下拉菜单
	 */
	@PostMapping("/version/newFirmware/productName")
	public R GetAllProductName(){
		List productNamelist = service.GetAllProductNameList();
		return R.data(productNamelist);
	}
	/**
	 *在固件表中新增固件
	 */
	@PostMapping("/version/newFirmware")//上传两个相同的文件会覆盖吗
	public R AddNewFirmware(@RequestParam String firmwareName, String firmwareVersion, String productName, String firmwareSign, String describe, @RequestPart("file") MultipartFile file){
		try{
			String firmwareFile = fileFeign.uploadFile(file);
			int isAdd = service.AddNewFirmware(firmwareName, firmwareVersion, productName, firmwareSign, describe, firmwareFile);
			switch (isAdd) {
				case 1:
					return R.data("上传成功");
				case 2:
					return R.fail("该产品已有此版本号的固件");
				default:
					return R.fail("上传失败");
			}
		}catch(Exception e){
			return R.fail("上传失败");
		}
	}
	/**
	 *固件验证，stats为验证返回值，1为成功，0为失败，现默认为1，
	 * 改变固件表中升级方式和验证状态
	 */
	@PostMapping("/version/firmwareVerify")
	public R VerifyFirmware(@RequestParam String firmwareId, int upgradeType, String deviceName, Double firmwareVersion){//upgradeType整包为0差分为1
		int stats=1;//1为成功，0为失败
		if (stats == 0){
			return R.data("验证失败");
		}else {
			boolean isUpdate = service.UpdateStats(firmwareId, upgradeType);
			if (isUpdate){
				R.status(true).setMsg("验证成功");
				return R.data("验证成功");
			}else {
				return R.data("验证成功，更新失败");
			}
		}
	}
	/**
	 *固件验证中设备名称下拉菜单，
	 * productId为固件所属产品
	 */
	@PostMapping("/version/firmwareVerify/device")
	public R GetdeviceNameList(@RequestParam String productId){
		List deviceNameList = service.GetdeviceNameList(productId);
		return R.data(deviceNameList);
	}
	/**
	 *固件删除
	 */
	@PostMapping("/version/delFirmware")
	public R DelFirmwareList(@RequestParam String firmwareId){
		boolean isDel = service.DelFirmwareList(firmwareId);
		return R.status(isDel);
	}
}
